using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YixiaoAdmin.IServices;
using YixiaoAdmin.Models;
using YixiaoAdmin.Common;

//这是 Users 控制器

namespace YixiaoAdmin.WebApi.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IUsersServices _UsersServices;


        public UsersController(IUsersServices UsersServices)
        {
            _UsersServices = UsersServices ?? 
                                       throw new ArgumentNullException(nameof(UsersServices));
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
       [HttpGet("action")]
       public async Task<string> Login(string Name, string passWord)
        {
            return await _UsersServices.Login(Name, passWord);
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<IList<Users>> All()
        {
            return await _UsersServices.Query();
        }
        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="queryPageModel">查询模型</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ActionResult<IEnumerable<Users>>> Pages(QueryPageModel queryPageModel)
        {
            return Ok(await _UsersServices.QueryPages(queryPageModel));
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Users> Get(string Id)
        {
            return await _UsersServices.QueryById(Id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Post(Users viewModel)
        {
            if (viewModel.Name.Length!= 0||viewModel.passWord.Length!= 0)
            {
                var Judge = (await _UsersServices.Query(x => x.Name == viewModel.Name)).FirstOrDefault();
                if(Judge == null)
                {
                    return await _UsersServices.Add(viewModel);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> Put(Users viewModel)
        {
            return await _UsersServices.Update(viewModel);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> Delete(string Id)
        {
            return await _UsersServices.RemoveById(Id);
        }
    }
}
